package util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class CustomClassLoader extends ClassLoader {

    public Class defineClass(String className, byte[] bytes) {
        return defineClass(className, bytes, 0, bytes.length);
    }

    public Object invokeInstance(Class clazz, Class[] parameterTypes, Object[] initArgs) {
        Object instance = null;
        try {
            Constructor constructor = clazz.getDeclaredConstructor(parameterTypes);
            constructor.setAccessible(true);
            instance = constructor.newInstance(initArgs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }

    public void invokeMethod(Class clazz, Object instance, Class[] parameterTypes, String methodName, Object[] methodArgs) {
        try {
            Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
            method.setAccessible(true);
            method.invoke(instance, methodArgs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
